<?php

/**
 * 
 * @author Nguyen Van Nhieu <nhieunv@evolable.asia>
 * Mar 22, 2013
 * File: queue_email.php
 *
 **/ 
class QueueEmail extends AppModel {
	var $name = 'QueueEmail';
	var $useTable = 'queue_email';
	const EMAIL_TYPE_COURSE = 'course';
	const EMAIL_STATUS_NOT_YET_SEND = 0;
	const EMAIL_STATUS_SENDING = 1;
	const EMAIL_STATUS_SENT = 2;
	
	/**
	 * 
	 * @return data
	 */
	function getCourseEmailListToSend() {
		try{
			$result = $this->find('all', array(
				'conditions' => array(
					$this->alias.'.status = '. QueueEmail::EMAIL_STATUS_NOT_YET_SEND,
					$this->alias.".email_type ='". QueueEmail::EMAIL_TYPE_COURSE . "'"
				),
				'limit' => Configure::read('EmailShel.number_of_record_for_one_send'),
			));
			return $result;
		}  catch (Exception $ex){
			return array();
		}
	}
	
	/**
	 * delete old queue emails, older time_to_delete_email
	 */
	function deleteOldEmailData() {
		$time = strtotime("-". Configure::read('Email.time_to_delete_email'). " day");
		$this->deleteAll(array(
			$this->alias.".email_type ='". QueueEmail::EMAIL_TYPE_COURSE . "'",
			$this->alias.".created <= '".$time."'",
			$this->alias.".status = ". QueueEmail::EMAIL_STATUS_SENT
		));
	}
	
}